/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import index.Element;
import index.ElementHandler;
import index.ElementTypeHandler;
import index.FullWord;
import index.Word;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import sql.SQLInterface;

/**
 *
 * @author Lu
 */
public class IndexElementTypes {

    public static final int NB_DOCS = 103;

    public static void main(String[] args) throws XPathExpressionException {
        SAXParserFactory fabrique = SAXParserFactory.newInstance();
        SAXParser parseur;
        try {
            parseur = fabrique.newSAXParser();
            DefaultHandler gestionnaire = new ElementTypeHandler();


            SQLInterface.init();
            String[] tables = new String[1];
            tables[0] = "elementtype";
            SQLInterface.cleanDatabase(tables);

            String[] documents = new String[NB_DOCS];
            for (int i = 1; i <= NB_DOCS; i++) {
                if (i < 10) {
                    documents[i - 1] = "..\\Collection\\d00" + i + ".xml";
                } else if (i < 100) {
                    if (i == 40 || i == 41 || i == 42) {
                        continue;
                    }
                    documents[i - 1] = "..\\Collection\\d0" + i + ".xml";
                } else {
                    documents[i - 1] = "..\\Collection\\d" + i + ".xml";
                }
            }

            for (String doc : documents) {
                if (doc != null) {
                    File f = new File(doc);
                    parseur.parse(f, gestionnaire);
                }
            }

            for (String elementType : ((ElementTypeHandler) gestionnaire).getTypeList()) {
                SQLInterface.addElementType(elementType);
            }

            System.out.println(((ElementTypeHandler) gestionnaire).getTypeList().size() + " element types indexed.");

        } catch (ParserConfigurationException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TestOnOneDoc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
